package  
{
	import flash.events.MouseEvent;
	import flash.text.TextField;
	/**
	 * ...
	 * @author patsup
	 * 
	 * 		This class executes the pirates in PiratePlank if their "values" (sum of their numbers)
	 * is equal to the values of DieObjects in DiceArea.  If not, automatically moves the PirateUnits
	 * from PiratePlank back to PirateLocation.  It also checks for the "end of round" state.
	 * (You "won" i.e. scored 0 if there are no pirates left in PirateLineup)
	 * 
	 */
	public class ExecuteButton extends Button 
	{
		//execute button image parameters
		private const EB_COLOR:int = 0xFF0000;
		private const EB_ALPHA:Number = 0.5;
		private const EB_WIDTH:int = 120;
		private const EB_HEIGHT:int = 40;
		
		private var btnLabel:TextField;			//label "Execute"
		
		private var gameRef:PGame;
		
		public function ExecuteButton(gameRef:PGame, xCoord:int, yCoord:int) 
		{
			name = "Execute Button";
			this.gameRef = gameRef;
			
			btnLabel = new TextField();
			
			InitImage();
			Initialize();
			super(xCoord, yCoord);
		}
		
		//Initialize()
		//Initializes the creation of the ExecuteButton
		public override function Initialize():void
		{
			DisableButton();		//cannot be clicked at start of round
			super.Initialize();
		}
		
		//InitImage()
		//Initializes the image of the ExecuteButton
		protected override function InitImage():void
		{
			graphics.beginFill(EB_COLOR, EB_ALPHA);
			graphics.drawRect(0, 0, EB_WIDTH, EB_HEIGHT);
			graphics.endFill();
			
			btnLabel.text = "Execute";
			btnLabel.width = btnLabel.textWidth*1.2;			//reduce the textfield's dimensions so they don't inflate the stage
			btnLabel.height = btnLabel.textHeight*1.5;
			btnLabel.x = 0.5 * EB_WIDTH - 0.5 * btnLabel.width;
			btnLabel.y = 0.5 * EB_HEIGHT - 0.5 * btnLabel.height;
			addChild(btnLabel);								//now add this txt label to the display list
			
			super.InitImage();
		}
		
		//InputMouseClick()
		//Takes action for ExecuteButton upon hearing the mouseclick event
		protected override function InputMouseClick(e:MouseEvent):void
		{
			//check the plank to see if it matches the dice
			if (gameRef.GetPlankValues() != gameRef.GetDiceValue())
			{
				trace ("Execute:" + gameRef.GetPlankValues() + "=" + gameRef.GetDiceValue() + "? Fail:No match!");
				 gameRef.ReturnPlank();									//return them to the lineup
			} else {
				trace("Execute:" + gameRef.GetPlankValues() + "=" + gameRef.GetDiceValue() + "? Success!");
				gameRef.RemovePlank();									//execute the pirates
				gameRef.EnableDice();		//decide whether to use dice or rollbutton(?)
				//mainRef.EnableRoll();									//let roll button be clickable
				DisableButton();										//don't let execute be clickable
				gameRef.DisablePirates();								//don't let pirates left on lineup be clickable
				if (gameRef.NumPirates() == 0) {						//check end game state
					gameRef.EndRound(1);								// <<<END OF ROUND>>>  1 = round won
				}
				gameRef.RefreshSessionText();							//update txt on session stats
			}
			
			super.InputMouseClick(e);
		}
	}

}